<!-- 失联修复 -->
<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="项目名称" name="name">
						<a-input v-model:value="searchFormState.name" placeholder="请输入修复批次的名字" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="状态：" name="status">
						<!--<a-input v-model:value="searchFormState.status" placeholder="请输入状态：" />-->
						<!--0已上传未进行修复，1表示已经进行修复中，2表示修复成功，3表示修复失败-->
						<a-select
							v-model:value="searchFormState.status"
							placeholder="请选择状态："
							:options="statusquery"
							:allowClear="true"
						>
							<a-select-option v-for="item in statusquery" :key="item.value" :value="item.value"></a-select-option>
						</a-select>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="创建人的昵称" name="createNickName">
						<a-input v-model:value="searchFormState.createNickName" placeholder="请输入创建人的昵称" />
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-button type="primary" @click="table.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-space>
						<a-button type="primary" @click="upload.openUpload()">
							<template #icon>
								<import-outlined />
							</template>
							导入
						</a-button>
						<!-- <a-button type="primary" @click="getAllRepairResult()">查询结果</a-button> -->
					</a-space>
					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('gzxRepairPhonenumberAdd')">
						<template #icon>
							<plus-outlined />
						</template>
						新增
					</a-button>
					<xn-batch-button
						v-if="hasPerm('gzxRepairPhonenumberBatchDelete')"
						buttonName="批量删除"
						icon="DeleteOutlined"
						:selectedRowKeys="selectedRowKeys"
						@batchCallBack="deleteBatchGzxRepairPhonenumber"
					/>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<!--<a @click="formRef.onOpen(record)" v-if="hasPerm('gzxRepairPhonenumberEdit')">详情</a>-->
						<a-divider
							type="vertical"
							v-if="hasPerm(['gzxRepairPhonenumberEdit', 'gzxRepairPhonenumberDelete'], 'and')"
						/>
						<a-popconfirm title="确定要删除吗？" @confirm="deleteGzxRepairPhonenumber(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('gzxRepairPhonenumberDelete')"> 删除 </a-button>
						</a-popconfirm>
						<a-button type="primary" size="small" @click="detaillist.onOpen(record)">修复结果</a-button>
						<a-button v-if="record.status === 0" type="primary" size="small" @click="repair(record)">
							开始进行修复
						</a-button>
						<a-button type="primary" size="small" @click="exportExcel(record)">导出</a-button>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Detaillist ref="detaillist" />
	<Form ref="formRef" @successful="table.refresh(true)" />
	<Upload ref="upload" @successful="table.refresh(true)" />
</template>

<script setup name="repairphonenumber">
	import Form from './form.vue'
	import gzxRepairPhonenumberApi from '@/api/tjzy/repair/repair/gzxRepairPhonenumberApi'
	import Detaillist from './detailList.vue'
	import Upload from '@/views/tjzy/repair/repair/upload.vue'

	const upload = ref()
	let searchFormState = reactive({})
	const searchFormRef = ref()
	const table = ref()
	const formRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	const columns = [
		{
			title: '项目名',
			dataIndex: 'name'
		},
		{
			title: '移动批次',
			dataIndex: 'batchId'
		},
		{
			title: '联通批次',
			dataIndex: 'ltBatchNumber'
		},
		{
			title: '联通状态',
			customRender: ({ text }) => getStatus(text),
			dataIndex: 'ltStatus'
		},
		{
			title: '移动状态',
			customRender: ({ text }) => getStatus(text),
			dataIndex: 'ydStatus'
		},
		{
			title: '创建人',
			dataIndex: 'createNickName'
		}
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['gzxRepairPhonenumberEdit', 'gzxRepairPhonenumberDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: '150px'
		})
	}
	const getAllRepairResult = () => {
		gzxRepairPhonenumberApi.getAllRepairResult()
		table.value.refresh(true)
	}
	const detaillist = ref()
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		return gzxRepairPhonenumberApi.gzxRepairPhonenumberPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		table.value.refresh(true)
	}
	// 删除
	const deleteGzxRepairPhonenumber = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		gzxRepairPhonenumberApi.gzxRepairPhonenumberDelete(params).then(() => {
			table.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchGzxRepairPhonenumber = (params) => {
		gzxRepairPhonenumberApi.gzxRepairPhonenumberDelete(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}
	const statusquery = ref([
		{
			label: '已上传未进行修复',
			value: '0'
		},
		{
			label: '已修复',
			value: '1'
		}
	])
	const status = ref([
		{
			label: '已上传未进行修复',
			value: '0'
		},
		{
			label: '修复中',
			value: '1'
		},
		{
			label: '修复成功',
			value: '2'
		},
		{
			label: '修复失败',
			value: '3'
		}
	])
	const getStatus = (value) => {
		let label = ''
		status.value.forEach((item) => {
			if (item.value === value) {
				label = item.label
			}
		})
		return label
	}

	const exportExcel = (record) => {
		let params = {
			id: record.id
		}
		gzxRepairPhonenumberApi.exportExcel(params).then((res) => {
			// 创建一个 URL 对象用于临时访问文件
			const url = window.URL.createObjectURL(new Blob([res.data]))
			// 创建一个隐藏的 <a> 元素用于触发下载
			const link = document.createElement('a')
			link.href = url
			link.setAttribute('download', '修复结果' + '.xlsx') // 设置下载文件名
			document.body.appendChild(link)
			link.click()
			// 清理工作：移除 <a> 元素和释放 URL 对象
			link.remove()
			window.URL.revokeObjectURL(url)
		})
	}

	const repair = (record) => {
		let params = {
			id: record.id
		}
		gzxRepairPhonenumberApi.repair(params).then(() => {
			table.value.refresh(true)
		})
	}
</script>
